package com.gexiaobao.pigeon.ui.fragment.mall

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentAfterSalesDetailsBinding
import com.gexiaobao.pigeon.ui.adapter.ReturnProgressAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/8/29 13:49
 * @Describe    : 退款详情
 */
class FragmentAfterSalesDetails : BaseFragment<MallViewModel, FragmentAfterSalesDetailsBinding>() {

    private val mAdapter: ReturnProgressAdapter by lazy { ReturnProgressAdapter(arrayListOf()) }

    private var orderId = 0
    private var shopId = 0
    private var goodsId = ""
    private lateinit var ivStatus: AppCompatImageView
    private var isReturnFinish = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose(getString(R.string.Refund_details)) {
            nav().navigateUp()
        }

        orderId = requireArguments().getInt("orderId")
        shopId = requireArguments().getInt("shopId")
        goodsId = requireArguments().getString("goodsId").toString()
        mDatabind.rvReturnDetail.init(LinearLayoutManager(context), mAdapter).addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 0f)))
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        onRefresh()
    }

    private fun onRefresh() {
        showLoadingAntiShake(getString(R.string.loading))
        mViewModel.getReviewStatus(orderId)
        mViewModel.getAfterSaleInfo(orderId)
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        mViewModel.afterInfoDetailResult.observe(viewLifecycleOwner) {
            mDatabind.data = it
            ImageLoaderManager.loadRoundImageOrEmpty(context, it.specImg, mDatabind.afterSalesImageIcon, 10, R.drawable.shop_product_def)
            when (it.orderStatus) {
                11 -> mDatabind.tvReturnMoneyHint.text = getString(R.string.wait_for_Refund)
                13 -> mDatabind.tvReturnMoneyHint.text = getString(R.string.click_review)
                14 -> mDatabind.tvReturnMoneyHint.text = getString(R.string.wait_for_merchant_review)
                15 -> mDatabind.tvReturnMoneyHint.text = getString(R.string.Wait_for_the_buyer_to_mail)
                7 -> mDatabind.tvReturnMoneyHint.text = getString(R.string.refunded_to_the_account)
                else -> {
                    mDatabind.tvReturnMoneyHint.text = ""
                }
            }
        }
        mViewModel.reViewsResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            if (!it.isEmpty()) {
                val footer = LayoutInflater.from(context).inflate(R.layout.view_logic_progress_header, null)
                ivStatus = footer.findViewById(R.id.ivStatus)
                mAdapter.addFooterView(footer)

                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
                isReturnFinish = it.isReturn
                if (isReturnFinish) {
                    ivStatus.visibility = View.GONE
                } else {
                    ivStatus.visibility = View.VISIBLE
                }
                it.list.forEachIndexed { _, listData ->
                    mDatabind.llRepeatSubmit.isVisible = listData.status == 3
                }
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvResubmit, mDatabind.conGoodsDetailInfo) {
            when (it) {
                mDatabind.tvResubmit -> {
                    nav().navigateAction(R.id.action_to_fragment_return_of_goods, Bundle().apply {
                        putInt("orderId", orderId)
                        putInt("shopUserId", shopId)
                        putString("goodsId", goodsId)
                    })
                }

                mDatabind.conGoodsDetailInfo -> {
                    nav().navigateAction(R.id.action_to_fragment_goods_detail, Bundle().apply {
                        putString("id", goodsId)
                    })
                }
            }
        }
    }
}